var WEBCHAT_BASE_URL = 'https://uchat.im-cc.com/webchat_new_ssl/static/';
var WEBCHAT_PC_URL = WEBCHAT_BASE_URL + 'google/index.html';
var WEBCHAT_IE_URL = WEBCHAT_BASE_URL + 'ie8/index.html';
var WEBCHAT_MOBILE_URL = WEBCHAT_BASE_URL + 'moblie/index.html';
var WEBCHAT_WX_URL = WEBCHAT_BASE_URL + 'wx/index.html';
let WebchatPop = (imnumber, hastab, isstatic, openid, params, box, callback) => {
  this.imnumber = imnumber;
  this.isstatic = isstatic;
  this.hastab = hastab || false;
  this.openid = openid || '';
  this.params = params || '';
  this.box = box || '';
  let that = this;
  var winW = 600;
  var winH = 450;
  var url = '';
  if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      navigator.userAgent)) {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) === 'micromessenger') {
      url = WEBCHAT_WX_URL;
    } else {
      url = WEBCHAT_MOBILE_URL;
    }
    url += '?ht=' + this.imnumber;
    if (this.openid) {
      url += '&openid=' + this.openid;
    }
    if (this.params) {
      url += '&params=' + this.params;
    }
    url += '&timestamp=' + new Date().getTime();
    window.location.href = url;
  } else {
    if (!this.box) {
      var mydiv = document.createElement('div');
      mydiv.style.display = 'none';
      mydiv.style.width = '600px';
      mydiv.style.height = '450px';
      mydiv.style.overflow = 'hidden';
      mydiv.style.margin = '1% auto';
      mydiv.id = 'adaptation';
      var iframe = document.createElement('iframe');
      iframe.src = '';
      iframe.id = 'mainiframe';
      iframe.width = '100%';
      iframe.height = '100%';
      iframe.frameBorder = 0;
      mydiv.appendChild(iframe);
      document.body.appendChild(mydiv);
    }
    url = WEBCHAT_PC_URL;
    if ((navigator.appName === 'Microsoft Internet Explorer' && navigator.appVersion.split(';')[1].replace(/[ ]/g, '') === 'MSIE8.0') || (navigator.appName === 'Microsoft Internet Explorer' && navigator.appVersion.split(';')[1].replace(/[ ]/g, '') === 'MSIE9.0') || navigator.userAgent.indexOf('Firefox') > 0) {
      url = WEBCHAT_IE_URL;
    }
    url += '?ht=' + this.imnumber;
    if (this.openid) {
      url += '&openid=' + this.openid;
    }
    if (this.hastab) {
      if (!this.box) {
        document.getElementById('adaptation').style.width = '900px';
        document.getElementById('adaptation').style.height = '600px';
      }
      winW = 900;
      winH = 600;
      url += '&hastab=' + this.hastab;
    }
    if (this.params) {
      url += '&params=' + this.params;
    }
    url += '&timestamp=' + new Date().getTime();
    if (this.isstatic && this.isstatic === 'true') {
      window.location.href = url;
    } else if (this.box) {
      var paramsStr = 'height=' + winH + ',width=' + winW +
        ',toolbar=0,scrollbars=0,location=0,menubar=0,resizable=1,top=50,left=200';
      window.open(url, '_blank', paramsStr);
    } else {
      document.getElementById('mainiframe').src = url;
    }
  }
  callback(that);
};
let imccShow = () => {
  document.getElementById('#adaptation').style.display = 'block';
};
export default{
  WebchatPop: WebchatPop,
  imccShow: imccShow
};
